<?
   class UploadMulti{
      var $string_name = '';
      var $error = '';
      function UploadMulti($upload_name,$file_path,$extend='',$size_allow,$new_name){
         foreach($_FILES[$upload_name]['tmp_name'] as $key => $tmp_name ){
            $new_file_name = $this->rename_image($new_name,$_FILES[$upload_name]['name'][$key]);
            $this->string_name .= $new_file_name.',';
            $file_size =$_FILES[$upload_name]['size'][$key];
            $file_tmp =$_FILES[$upload_name]['tmp_name'][$key];
            $file_type=$_FILES[$upload_name]['type'][$key];	
            if($file_size > ($size_allow * 1024)){
               $this->error ='File ảnh vượt quá dung lượng cho phép 2 MB';
            }		
            if(empty($this->error)==true){
               if(is_dir("$file_path/".$new_file_name)==false){
                   move_uploaded_file($file_tmp,"$file_path/".$new_file_name);
               }else{									// rename the file if another one exist
                   $new_dir="$file_path/".$new_file_name.time();
                    rename($file_tmp,$new_dir) ;				
               }	
            }else{
                   echo $this->error.'<br />';
            }
          }
          $this->string_name = substr($this->string_name,0,-1); // cắt dấu phảy cuối cùng
      }
      function generate_name($filename){
      	$name = "";
      	for($i=0; $i<3; $i++){
      		$name .= chr(rand(97,122));
      	}
      	$today= getdate();
      	$name.= $today[0];
      	$ext	= substr($filename, (strrpos($filename, ".") + 1));
      	return $name . "." . $ext;
      }
      function rename_image($new_name,$filename){
         $name = '';
         $ext	= substr($filename, (strrpos($filename, ".") + 1));
         $name = str_replace(' ','-',removeAccent($new_name)).'-'.rand(11,99);
         $name .= '.' . $ext;
         return $name;
      }
   }
?>